package org.adream.account.rest.api;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.adream.account.entity.TagEntity;
import org.adream.account.model.ResultModel;
import org.adream.account.rest.api.model.TagSearchForm;
import org.adream.account.service.TagService;
import org.adream.account.util.Constant;
import org.adream.account.util.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;

/**
 * 后台管理标签所需的接口
 * 
 * @author chenhaoyu
 *
 */

@Controller
public class TagApiController {

	private final static Logger logger = LoggerFactory.getLogger(TagApiController.class);

	@Autowired
	private TagService tagService;
	
	/**
	 * 增
	 * 
	 * @param tagEntity
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/tag", produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
	public ResultModel<String> addTag(@RequestBody TagEntity tagEntity) {
		if(tagEntity == null || StringUtils.isEmpty(tagEntity.getTagName())) {
			return new ResultModel<String>(ResultModel.ERR_PARAM, "新增失败,请求参数有误", null);
		}
		return tagService.addTag(tagEntity);
	}
	
	/**
	 * 删除
	 * @param id  tag id
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/tag/{id}", produces = "application/json;charset=UTF-8", method = RequestMethod.DELETE)
	public ResultModel<String> deleteTag(@PathVariable("id") String id) {
		if(StringUtils.isEmpty(id)) {
			return new ResultModel<String>(ResultModel.ERR_PARAM, "删除失败,请求参数有误", null);
		}
		return tagService.deleteTag(id);
	}
	
	
	/**
	 * 批量删除
	 * @param tagIds
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/tag/deleteTags", produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
	public ResultModel<String> deleteTag(@RequestBody String[] tagIds) {
		if(tagIds == null || tagIds.length == 0) {
			return new ResultModel<String>(ResultModel.ERR_PARAM, "批量删除失败,请求参数有误", null);
		}
		return tagService.deleteTags(tagIds);
	}

	/**
	 * 改
	 * @param id
	 * @param tagEntity
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/tag/{id}", produces = "application/json;charset=UTF-8", method = RequestMethod.PUT)
	public ResultModel<String> updateTag(@PathVariable("id") String id,@RequestBody TagEntity tagEntity) {
		if(StringUtils.isEmpty(id) || tagEntity == null || StringUtils.isEmpty(tagEntity.getTagName())) {
			return new ResultModel<String>(ResultModel.ERR_PARAM, "请求参数有误", null);
		}
		return tagService.updateTag(id,tagEntity);
	}
	
	/**
	 * 根据tagId查询tag
	 * @param id tag id
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/tag/{id}", produces = "application/json;charset=UTF-8",method = RequestMethod.GET)
	public ResultModel<TagEntity> queryTagById(@PathVariable("id") String id) {
		if(StringUtils.isEmpty(id)) {
			return new ResultModel<TagEntity>(ResultModel.ERR_PARAM, "请求参数有误", null);
		}
		TagEntity tagEntity = tagService.queryTagById(id);
		if(tagEntity == null) {
			logger.warn("标签数据不存在,tagId:{}", id);
			return new ResultModel<TagEntity>(ResultModel.NOT_FOUND_OBJECT, "查询失败,数据不存在", null);
		}
		return new ResultModel<TagEntity>(ResultModel.SUCCESS, "成功查询tag", tagEntity);
	}
	
	/**
	 * 查询tag
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/tag", produces = "application/json;charset=UTF-8",method = RequestMethod.GET)
	public ResultModel<List<TagEntity>> queryTagsByDr() {
		List<TagEntity> tags = tagService.queryTagsByDr(Constant.ONE);
		if(Utils.isEmptyCollection(tags)) {
			return new ResultModel<List<TagEntity>>(ResultModel.NOT_FOUND_OBJECT, "查询失败,数据不存在", null);
		}
		return new ResultModel<List<TagEntity>>(ResultModel.SUCCESS, "成功查询tag", tags);
	}
	
	/**
	 * 根据dr,tagName查询tag
	 * @param dr 删除标志
	 * @param tagName tag name
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/tag/queryTagByDrAndLikeTagName", produces = "application/json;charset=UTF-8")
	public ResultModel<?> queryTagByDrAndLikeTagName(Integer dr, String tagName) {
		if(dr == null) {
			return new ResultModel<String>(ResultModel.ERR_PARAM, "请求参数为空", null);
		}
		List<TagEntity> tags = new ArrayList<>();
		if(StringUtils.isEmpty(tagName) || StringUtils.trimWhitespace(tagName).length() == Constant.ZERO) {
			tags = tagService.queryTagsByDr(dr);   //若tagName为空,查询全部
		} else {
			tags = tagService.queryTagsByDrAndLikeTagName(dr, tagName);
		}
		if(Utils.isEmptyCollection(tags)) {
			return new ResultModel<List<TagEntity>>(ResultModel.NOT_FOUND_OBJECT, "查询失败,数据不存在", null);
		}
		return new ResultModel<List<TagEntity>>(ResultModel.SUCCESS, "成功获取tag", tags);
	}
	
	/**
	 * @param resName resource name
	 * @return
	 */
	@RequestMapping(value = "/tag/checkTagName")
	public void checkTagName(String tagName,HttpServletResponse response) {
		boolean result = tagService.isValidTagName(tagName);
		PrintWriter out = null;
		try {
			out = response.getWriter();
			out.write(String.valueOf(result));
		} catch (IOException e) {
			logger.warn("检查tagName出错:)");
		} finally {
			if(out != null) {
				out.close();
			}
		}
	}
	
	/**
	 * 分页查询
	 * @param form 标签搜索表单
	 * @return
	 */
	@RequestMapping(value = "/tag/selectPageTag", produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
	@ResponseBody
	public ResultModel<?> selectPageTag(@RequestBody TagSearchForm form) {
		if(form == null || StringUtils.isEmpty(form.getDr())) {
			return new ResultModel<String>(ResultModel.ERR_PARAM, "查询失败,请求参数有误", null);
		}
		PageInfo<TagEntity> page = tagService.selectPageTag(form.getDr(), form.getTagName(), form.getPageNo(), form.getPageSize());
		return new ResultModel<PageInfo<TagEntity>>(ResultModel.SUCCESS, "查询成功", page);
	}
}